import {
  MessageBox,
  Indicator
} from 'mint-ui';
import router from '../router'

//生成手机唯一标识
const guid = () => {
  function S4() {
    return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
  }
  return (S4() + S4() + S4() + S4() + S4() + S4() + S4() + S4());
}

const getGuid = () => {
  if (!localStorage.guid) {
    localStorage.guid = guid()
  }
  return localStorage.guid
}

const downloadApp = (data) => {
  Indicator.open({
    text: '此功能暂对APP用户开放',
    spinnerType: 'fading-circle'
  });
  setTimeout(() => {
    Indicator.close();
    router.push({
      path: '/download',
      query: {
        type: data.type
      }
    })
  }, 2000)
}

//判断安卓IOS
const isAndroidOrIos = () => {
  var ua = navigator.userAgent.toLowerCase();
  //Android终端
  var isAndroid = ua.indexOf('android') > -1 || ua.indexOf('adr') > -1;
  //Ios终端
  var isiOS = !!ua.match(/\(i[^;]+;( u;)? cpu.+mac os x/);
  return {
    isAndroid,
    isiOS
  };
}


const isWeixin = () => {
  var ua = navigator.userAgent.toLowerCase();
  if (ua.match(/MicroMessenger/i) == 'micromessenger') {
    return true;
  } else {
    return false;
  }
}

function openUrlPay(url, data) {

  var w = plus.webview.create(url, 'pay', {}, data);

  w.setStyle({
    'zindex': 30,
    'titleNView': {
      'backgroundColor': '#FFF',
      'titleText': '',
      'titleColor': '#CCCCCC',
      autoBackButton: false,
      buttons: [{
        type: 'back',
        text: '0000',
        float: 'left',
        onclick: function () {
          w.canBack(function (e) {
            if (e.canBack) {
              w.back();
            } else {
              w.close(); //hide,quit
              //plus.runtime.quit();
            }

          })
        }
      }]

    }
  })
  w.evalJS("plus.key.addEventListener('backbutton',function () { plus.webview.currentWebview().close(); })");
  w.show(); // 显示窗口
}

function openUrl(url) {
  if (window.Judge) {
    openUrlPay(url, {})
  } else {
    window.open(url)
  }
}

//字符串进行加密   
function compileStr(code) {
  var c = String.fromCharCode(code.charCodeAt(0) + code.length);
  for (var i = 1; i < code.length; i++) {
    c += String.fromCharCode(code.charCodeAt(i) + code.charCodeAt(i - 1));
  }
  return escape(c);
}
//字符串进行解密   
function uncompileStr(code) {
  code = unescape(code);
  var c = String.fromCharCode(code.charCodeAt(0) - code.length);
  for (var i = 1; i < code.length; i++) {
    c += String.fromCharCode(code.charCodeAt(i) - c.charCodeAt(i - 1));
  }
  return c;
}




function format(shijianchuo) {
  //shijianchuo是整数，否则要parseInt转换
  var time = new Date(shijianchuo);
  var y = time.getFullYear();
  var m = time.getMonth() + 1;
  var d = time.getDate();
  var h = time.getHours();
  var mm = time.getMinutes();
  var s = time.getSeconds();
  return y + '-' + add0(m) + '-' + add0(d) + ' ' + add0(h) + ':' + add0(mm) + ':' + add0(s);
}

function add0(m) {
  return m < 10 ? '0' + m : m
}


export {
  isWeixin,
  getGuid,
  downloadApp,
  isAndroidOrIos,
  openUrl,
  openUrlPay,
  compileStr,
  uncompileStr,
  format

}
